package com.gxuwz.restaurant.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.restaurant.common.Result;
import com.gxuwz.restaurant.entity.WineType;
import com.gxuwz.restaurant.mapper.WineTypeMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/wineType")
public class WineTypeController {
    @Resource
    WineTypeMapper wineTypeMapper;
    @GetMapping("/selectType")   //get用来查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        System.out.println("执行酒水类型");
        LambdaQueryWrapper<WineType> wrapper = Wrappers.<WineType>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            wrapper.like(WineType::getWineType,search);
        }
        Page<WineType> WineTypePage =  wineTypeMapper.selectPage(new Page<>(pageNum,pageSize), wrapper);
        return Result.success(WineTypePage );
    }
    @GetMapping("/selectTypelist")   //get用来查询
    public Result<?> findlist(){
        List typelist =  wineTypeMapper.selectList(null);

        return Result.success(typelist );
    }

    @PostMapping("/addType")//post用来新增
    public Result<?> save(@RequestBody WineType wineType){
        System.out.println("执行添加分类");

        wineTypeMapper.insert(wineType);

        return Result.success();
    }
    @PutMapping ("update")//put用来更新
    public Result<?> update (@RequestBody WineType wineType){
        System.out.println("执行更新");

        wineTypeMapper.updateById(wineType);

        return Result.success();


    }

    @DeleteMapping("/{id}") //DELETE用来删除
    public Result<?> delete(@PathVariable long id){
        System.out.println("执行删除方法");
        wineTypeMapper.deleteById(id);

        return Result.success();


    }
}
